package com.yklx.youke.admin.controller;

import com.yklx.youke.util.ApiResponse;
import com.yklx.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
public class AdminStatsController {
    
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 获取管理员统计数据
     */
    @GetMapping("/stats")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getStats(@RequestHeader("Authorization") String token) {
        try {
            String userId = extractUserIdFromToken(token);
            if (userId == null) {
                return ResponseEntity.status(401).build();
            }
            
            // 简化的统计数据（在实际应用中会查询数据库）
            Map<String, Object> stats = new HashMap<>();
            stats.put("totalUsers", 1247);
            stats.put("activeTeachers", 128);
            stats.put("pendingTeachers", 23);
            stats.put("totalSessions", 4562);
            
            ApiResponse<Map<String, Object>> response = ApiResponse.success(stats);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<Map<String, Object>> response = ApiResponse.error("Failed to load stats");
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 获取最近用户数据
     */
    @GetMapping("/users/recent")
    public ResponseEntity<ApiResponse<Object>> getRecentUsers(@RequestHeader("Authorization") String token) {
        try {
            String userId = extractUserIdFromToken(token);
            if (userId == null) {
                return ResponseEntity.status(401).build();
            }
            
            // 返回模拟数据（在实际应用中会查询数据库）
            Object[] mockUsers = new Object[]{
                Map.of("userId", "1", "username", "teacher1", "email", "teacher1@youke.com", "roleId", "TEACHER", "createdAt", "2025-11-01T10:30:00"),
                Map.of("userId", "2", "username", "parent1", "email", "parent1@youke.com", "roleId", "PARENT", "createdAt", "2025-11-02T14:20:00"),
                Map.of("userId", "3", "username", "teacher2", "email", "teacher2@youke.com", "roleId", "TEACHER", "createdAt", "2025-11-03T09:15:00"),
                Map.of("userId", "4", "username", "admin1", "email", "admin1@youke.com", "roleId", "ADMIN", "createdAt", "2025-11-04T16:45:00"),
                Map.of("userId", "5", "username", "parent2", "email", "parent2@youke.com", "roleId", "PARENT", "createdAt", "2025-11-05T11:30:00")
            };
            
            ApiResponse<Object> response = ApiResponse.success(mockUsers);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<Object> response = ApiResponse.error("Failed to load recent users");
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 获取待审核教师数据
     */
    @GetMapping("/teachers/pending")
    public ResponseEntity<ApiResponse<Object>> getPendingTeachers(@RequestHeader("Authorization") String token) {
        try {
            String userId = extractUserIdFromToken(token);
            if (userId == null) {
                return ResponseEntity.status(401).build();
            }
            
            // 返回模拟数据（在实际应用中会查询数据库）
            Object[] mockTeachers = new Object[]{
                Map.of("userId", "6", "username", "newteacher1", "email", "newteacher1@youke.com", "university", "Beijing University"),
                Map.of("userId", "7", "username", "newteacher2", "email", "newteacher2@youke.com", "university", "Shanghai Jiao Tong University"),
                Map.of("userId", "8", "username", "newteacher3", "email", "newteacher3@youke.com", "university", "Tsinghua University")
            };
            
            ApiResponse<Object> response = ApiResponse.success(mockTeachers);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<Object> response = ApiResponse.error("Failed to load pending teachers");
            return ResponseEntity.status(500).body(response);
        }
    }

    // 辅助方法
    private String extractUserIdFromToken(String token) {
        if (token != null && token.startsWith("Bearer ")) {
            String actualToken = token.substring(7);
            try {
                return jwtUtil.getUserIdFromToken(actualToken);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }
}